import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../helper/routeHelper.dart';
import '../../pages/docsend/docsend_add.dart';
import '../../widgets/dialogWidget.dart';

import '../../helper/uidata.dart';
import '../../model/docsend.dart';
//import 'DocSend_authorty.dart';
import '../../widgets/someCards.dart';

class DocSendListPage extends StatefulWidget {
  final ifAppbar;
  @override
  DocSendListPage(this.ifAppbar);
  @override
  DocSendListPageState createState() => new DocSendListPageState();
}

class DocSendListPageState extends State<DocSendListPage> {
  List<DocSend> RList = <DocSend>[];
  bool isEnd = false;
  ScrollController _scrollController = new ScrollController();
  bool firstLoading = true;
  bool ifCanAdd = true;
  int canShow = 0;

  @override
  void initState() {
    super.initState();

    if (UIData.CurrentUser!.Department == "水调中心") {
      canShow = 0;
    } else {
      canShow = 1;
    }

    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        getMore();
      }
    });

    getNewer();
  }

  Future<Null> delete(DocSend r1) async {
    // await addOne();

    await DocSend.Delete(r1);
    setState(() {
      RList.remove(r1);
    });
  }

  onDeleted(DocSend r1) => {
        setState(() {
          RList.remove(r1);
        })
      };

  Future<Null> getMore() async {
    // await addOne();
    int count = 10;
    if (isEnd == true) return;
    int page = RList.length ~/ count;
    var list1 = await DocSend.GetList(page + 1, count, canShow);
    var list2 =
        list1.where((p) => RList.any((x) => x.ID == p.ID) == false).toList();

    setState(() {
      if (list1.length < count) isEnd = true;
      RList.insertAll(RList.length, list2);
    });
  }

  Future<Null> getRefrush() async {
    var list1 = await DocSend.GetList(1, 15, canShow);

    setState(() {
      RList = list1;
    });
  }

  void addNew() {
    RouteHelper.ShowPage(context, DocSendAddPage());
  }

  Future<Null> getNewer() async {
    var list1 = await DocSend.GetList(1, 10, canShow);
    setState(() {
      firstLoading = false;
    });

    if (list1.length == 0) return;

    var list2 =
        list1.where((p) => RList.any((x) => x.ID == p.ID) == false).toList();
    if (list2.length == 0) return;

    list2.insertAll(list2.length, RList);

    setState(() {
      firstLoading = false;
      RList = list2;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (RList == null) return new Container();

    // TODO: implement build
    return new Scaffold(
        appBar: widget.ifAppbar
            ? AppBar(
                title: Text('发文审批'),
                actions: <Widget>[
                  UIData.CurrentUser!.Department == "水调中心"
                      ? ElevatedButton(
                          child: Icon(
                            Icons.add,
                            color: Colors.white,
                          ),
                          onPressed: addNew)
                      : Container(),
                  ElevatedButton(
                      child: Icon(
                        Icons.exit_to_app,
                        color: Colors.white,
                      ),
                      onPressed: () {
                        showDialog(
                            context: context,
                            builder: (BuildContext context) {
                              return DialogWidget.getDialog(
                                  context, "退出登录", "点击确定将退出登录", () {
                                RouteHelper.exitLogin(context);

                                ;
                              });
                            });
                      })
                ],
              )
            : null,
        floatingActionButton: FloatingActionButton(
            child: Icon(Icons.refresh), onPressed: getRefrush),
        body: firstLoading == true
            ? Center(
                child: SpinKitCircle(
                color: Colors.blue,
              ))
            : Container(
                margin: EdgeInsets.only(top: 8.0),
                child: RList.length == 0
                    ? Container()
                    : new RefreshIndicator(
                        child: ListView.builder(
                          itemCount: RList.length + 1,
                          controller: _scrollController,
                          physics: const AlwaysScrollableScrollPhysics(),
                          itemBuilder: (BuildContext context, int index) {
                            if (index != RList.length) {
                              return SomeCard.DocSendCard(
                                  context, RList[index], getRefrush);
                            } else {
                              if (isEnd == true) {
                                return Container(
                                    margin: EdgeInsets.all(12.0),
                                    child: Center(
                                        child: Text(
                                      "没有更多了",
                                      style: TextStyle(color: Colors.grey),
                                    )));
                              } else {
                                return new Container();
                              }
                            }
                          },
                        ),
                        onRefresh: getNewer,
                      )));
  }
}
